package controller;

import java.util.ArrayList;
import java.util.InputMismatchException;

import manager.CompletedCourseMgr;
import manager.SectionStudentMgr;
import manager.StudentMgr;
import entity.Course;
import entity.Section;
import entity.Student;
import exception.ValidationError;

public class StudentCtrl {
	private Student loggedInStudent;
	private StudentMgr studentMgr;
	private CompletedCourseMgr completedCourseMgr;
	private SectionStudentMgr sectionStudentMgr;

	public StudentCtrl() {

	}

	public boolean authenticateStudent(String userId, String password) {

		try {
			Student student = studentMgr.retrieveStudentForUserId(userId);
			if (student.getPassword().equals(password)) {
				loggedInStudent = student;
				return true;
			}
		} catch (ValidationError e) {
			// user doesn't exist in our database
		}

		return false;
	}

	public Student getLoggedInStudent() {
		return loggedInStudent;
	}

	public void addStudent(String userId, String password, String name,
			String school, double edollar) {

		studentMgr.addStudent(new Student(userId, password, name, school,
				edollar));
	}

	public void deductEdollarForStudent(double amountToDeduct, Student student) {
		if (amountToDeduct < 0) {
			throw new InputMismatchException("invalid e-dollar");
		} else if (!hasStudentEnoughEdollar(student, amountToDeduct)) {
			throw new InputMismatchException("insufficient e-dollar");
		} else {
			double newAmount = student.getEdollar() - amountToDeduct;
			studentMgr.updateEdollarForStudent(newAmount, student.getUserId());
			student.setEdollar(newAmount);
		}
	}

	private boolean hasStudentEnoughEdollar(Student student,
			double requiredAmount) {
		return student.getEdollar() >= requiredAmount;
	}

	public void refundEdollarForStudent(double amountToRefund, Student student) {
		if (amountToRefund < 0) {
			throw new InputMismatchException("invalid e-dollar");
		} else {
			double newAmount = student.getEdollar() + amountToRefund;
			studentMgr.updateEdollarForStudent(newAmount, student.getUserId());
			student.setEdollar(newAmount);
		}
	}

	public void deleteAllStudents() {
		studentMgr.deleteAllStudents();
	}

	public void addCompletedCourseForStudent(String courseCode, String userId) {
		completedCourseMgr.addCompletedCourseForStudent(courseCode, userId);
	}

	public ArrayList<Course> retrieveCompletedCourseForStudent(String userId) {
		return completedCourseMgr.retrieveCompletedCourseForStudent(userId);
	}

	public void deleteAllCompletedCourses() {
		completedCourseMgr.deleteAllCompletedCourses();
	}

	public void addSectionForStudent(Section section, Student student) {
		sectionStudentMgr
				.addSectionEnrollmentForStudent(section.getCourse()
						.getCourseCode(), section.getSectionCode(), student
						.getUserId());
	}

	public void dropSectionForStudent(String userId, String courseCode,
			String sectionCode) {
		sectionStudentMgr.deleteSectionEnrollmentForStudent(userId, courseCode,
				sectionCode);
	}

	public ArrayList<Section> retrieveSuccessfullyEnrolledSectionsForStudent(
			Student student) {
		return sectionStudentMgr.retrieveSectionEnrollmentsForStudent(student
				.getUserId());
	}

	public boolean hasStudent(String userId) {
		try {
			studentMgr.retrieveStudentForUserId(userId);
		} catch (ValidationError e) {
			return false;
		}
		return true;
	}

}
